<template>
  <basic-container>
    <avue-crud :option="option" :data="data" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
      @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
      @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="primary" :loading="isLoading" size="small" icon="el-icon-refresh" plain @click="SynchroWechatUser">同步数据
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import request from '@/router/axios';

  export default {
    data() {
      return {
        isLoading:false,
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          searchMenuSpan: 4,
          border: true,
          index: true,
          refreshBtn: false,
          excelBtn: true,
          addBtn: false,
          menuWidth: '150px',
          column: [
            {
              label: "昵称",
              prop: "nickname",
              search: true,
              editDisabled: true,
              width: 200
            },
            {
              label: "国家",
              prop: "country",
              editDisabled: true,
              width: 120
            },
            {
              label: "省",
              prop: "province",
              editDisabled: true,
              width: 120
            },
            {
              label: "市",
              prop: "city",
              editDisabled: true,
              width: 120
            },
            {
              label: "头像",
              prop: "headimgurl",
              dataType: 'string',
              type: 'img',
              editDisabled: true,
              width: 80
            },
            {
              label: "备注",
              prop: "remark"
            }
          ]
        },
        data: []
      };
    },
    created() {
    },
    methods: {
      SynchroWechatUser() {
        this.$confirm("确定同步数据?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            this.isLoading = true
            request({
              url: '/api/WeChat/SynchroWechatUser'
            }).then(res => {
              this.isLoading = false
              this.onLoad(this.page);
            });
          });
      },
      rowSave(row, loading, done) {
        request({
          url: '/api/WxUser/add',
          method: 'post',
          data: row
        }).then(() => {
          loading();
          this.onLoad(this.page);
          done();
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        return request({
          url: '/api/WxUser/edit',
          method: 'post',
          data: row
        }).then(() => {
          loading();
          this.onLoad(this.page);
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return request({
              url: '/api/WxUser/remove',
              method: 'post',
              params: {
                ids: row.id
              }
            });
          })
          .then(() => {
            this.onLoad(this.page);
          });
      },
      searchReset() {
        this.query = {};
        this.page.currentPage = 1;
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.page.currentPage = 1;
        this.query = params;
        this.onLoad(this.page);
        done();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      onLoad(page) {
        request({
          url: '/api/WxUser/getpages',
          method: 'get',
          params: {
            key: this.query.nickname == undefined ? '' : this.query.nickname,
            type: this.query.enterprise == undefined ? '' : this.query.enterprise,
            page: page.currentPage,
            limit: page.pageSize
          }
        }).then(res => {
          const data = res.data
          this.data = data.data;
          this.page.total = data.remark;
        });
      }
    }
  };
</script>

<style>
</style>
